﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace UCMainApp.Code
{
    public partial class DiffrentCode : Form
    {
        public string CurrentStr
        {
            get { return this.textEdit1.Text; }
            set { this.textEdit1.Text = value; }
        }

        private byte[] _source;
        private byte[] Source
        {
            get
            {
                if (_source==null)
                {
                    _source = AnalyseSRT(); 
                }
                return _source;
            }
        }
        public const string TargetStr = "字幕仅供翻译学习交流，谢绝用于任何商业用途";
        public DiffrentCode()
        {
            InitializeComponent();
           
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string name = "";
            string result = "";

           


            CovenEncoding(Encoding.GetEncoding("gbk"), out name, out result);
            lb11.Text = name;
            lb12.Text = result;

            CovenEncoding(Encoding.GetEncoding("gb2312"), out name, out result);
            lb21.Text = name;
            lb22.Text = result;

            CovenEncoding(Encoding.UTF32, out name, out result);
            lb31.Text = name;
            lb32.Text = result;

            CovenEncoding(Encoding.ASCII, out name, out result);
            lb41.Text = name;
            lb42.Text = result;

            CovenEncoding(Encoding.Unicode, out name, out result);
            lb51.Text = name;
            lb52.Text = result;

            CovenEncoding(Encoding.UTF8, out name, out result);
            lb61.Text = name;
            lb62.Text = result;
        

        }

      

        private void ShowEncoding(Encoding encoding  ,out string name,out string result)
        {
            string s = "";
            byte[] buffer = encoding.GetBytes(CurrentStr);
            name = encoding.HeaderName;
            result = Encoding.GetEncoding("utf-8").GetString(buffer);
        }
        private void CovenEncoding(Encoding encoding, out string name, out string result)
        {
            string[] encodeList = { "utf-8", "gb2312", "", "", "" };
            Encoding utf8 = Encoding.UTF8;
            byte[] results = Encoding.Convert(utf8, encoding, Source);
            name = encoding.HeaderName;
            result = encoding.GetString(results);
        }

       
        /// <summary>
        /// 返回uft-8解码后的字节码
        /// </summary>
        /// <returns></returns>
        private byte[] AnalyseSRT()
        {
            string[] strs = File.ReadAllLines("2.srt",Encoding.UTF8);

           
            byte[] buffer = UTF8Encoding.UTF8.GetBytes(strs[6]);

            return buffer;

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(ForeachEncoding);
            t.Start();
        }
        private void UpdateLst( string str)
        {

            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action<string>(UpdateLst), str);
            }
            else
            {
                listBox1.Items.Add(str);
            }
        }
        private void ForeachEncoding()
        {
            string name = "";
            string result = "";

            Stopwatch myWatch = new Stopwatch();
            myWatch.Start();

           
          
            for (int i = 0; i < 12000; i++)
            {
                Encoding encoding;
                try
                {
                    encoding = Encoding.GetEncoding(i);
                }
                catch (Exception ex)
                {
                    continue;
                }
              
                CovenEncoding(Encoding.GetEncoding("gbk"), out name, out result);
                if (result == TargetStr)
                {
                    UpdateLst("成功的code="+i+" >" + encoding.HeaderName + "=>" + result);
                    return;
                }
                else
                {
                    UpdateLst("失败的code=" + i + ">" + encoding.HeaderName + "=>" + result);
                }
            }

             myWatch.Stop();
            long myUseTime = myWatch.ElapsedMilliseconds / 1000;
            UpdateLst("執行時間: " + myUseTime.ToString() + "s"); 
        }

    }
}